package io.sage.modules.employer.controller;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import io.sage.common.msg.EmailUtils;
import io.sage.common.utils.CertVerifyUtils;
import io.sage.common.utils.R;
import io.swagger.annotations.ApiOperation;

/**
 * @author xujh
 * @email jhang_mail@163.com
 * @date 2018-07-09 13:48:15
 */
@RestController
@RequestMapping("msg")
public class MsgController {
	
	  /**
	   * 发送邮件
	   */
	  @PostMapping("send")
	  @ApiOperation("发送邮件")
	  public R info(@RequestBody JSONObject jsonObject){
		  //接收邮件账号
		  String account = jsonObject.getString("account");
		  //邮件标题
		  String title = jsonObject.getString("title");
		  //发送内容
		  String content = jsonObject.getString("content");
		  //校验参数.
		  if(StringUtils.isEmpty(account)|| account == null){
					
			 return R.error("邮箱账户不能为空");
		  }
		  if(StringUtils.isEmpty(title)|| title == null){
			  
			  return R.error("邮箱标题不能为空");
		  }
		  if(StringUtils.isEmpty(content)|| content == null){
			  
			  return R.error("邮件内容不能为空");
		  }
		  
		  boolean idNumber = CertVerifyUtils.emailFormat(account);
		  
		  if(!idNumber){
			  
			  return R.error("邮件格式不正确");
		  }
		  
		  boolean send = EmailUtils.send(account, title, content);
		  
		  if(send){
			  return R.ok("邮件发送成功");
		  }
		
		return R.error("邮件发送失败");
	  }

}
